using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BO;
using BL;
using Telerik.WinControls.UI;
namespace AnimalFarm.Livestock_manage
{
    public partial class UpdateLivestock : Telerik.WinControls.UI.RadForm
    {
       
        Livestock_Info selectedLivestock;
        B_LivestockType livestocTypekManager;
        B_Livestock livestockManager;
        B_Herd herdManager;
        B_HomeField homefieldManager;
        DataTable livestockdata;
        public UpdateLivestock()
        {
            InitializeComponent();
            livestockManager = B_Livestock.CreateInstance();
            showLivestock();
            livestocTypekManager = B_LivestockType.CreateInstance();
            livestockManager = B_Livestock.CreateInstance();
            herdManager = B_Herd.CreateInstance();
            homefieldManager = B_HomeField.CreateInstance();
            //bind live stock data
            bindLivestockData();
            //bind herd data
            bindHerdData();
            //bind home field data
            bindHomefieldData();
        }
        public void showLivestock()
        {
            livestockdata = livestockManager.GetAllLivestock();
            radGridView_livestock.DataSource = livestockdata;
        }
        public void bindLivestockData()
        {
            DataTable dt = livestocTypekManager.GetAllLivestockType();
            cm_livestockType.DataSource = dt;
            cm_livestockType.DisplayMember = "LivestockTypeName";
            cm_livestockType.ValueMember = "LivestockTypeID";
        }
        public void bindHerdData()
        {
            DataTable dt = herdManager.GetAllHerd();
            cm_herd.DataSource = dt;
            cm_herd.DisplayMember = "HerdName";
            cm_herd.ValueMember = "HerdID";

        }
        public void bindHomefieldData()
        {
            DataTable dt = homefieldManager.GetAllHomeField();
            cm_hfield.DataSource = dt;
            cm_hfield.DisplayMember = "HomeFieldName";
            cm_hfield.ValueMember = "HomeFieldID";
        }
        private void radGridView_livestock_MouseClick(object sender, MouseEventArgs e)
        {

           
            try
            {
                if (radGridView_livestock.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = radGridView_livestock.SelectedRows[0];
                    this.selectedLivestock = convertGridviewRowToLivestock(row);
                    showSelectedLivestockDetail();
                
                }
              
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        public Livestock_Info convertGridviewRowToLivestock(GridViewRowInfo row)
        {
           
            Livestock_Info ls_info = new Livestock_Info();
            ls_info.LivestockID =int.Parse( row.Cells["LivestockID"].Value.ToString());
            ls_info.LivestockTypeID=int.Parse( row.Cells["LivestockTypeID"].Value.ToString());
            ls_info.HomeFieldID = int.Parse (row.Cells["HomeFieldID"].Value.ToString());
            ls_info.MAFFid = row.Cells["MAFFid"].Value.ToString();
            ls_info.Blood=row.Cells["Blood"].Value.ToString();
            ls_info.Breed=row.Cells["HomeFieldID"].Value.ToString();
            ls_info.isAlive =bool.Parse( row.Cells["isAlive"].Value.ToString());
            ls_info.DOB=(DateTime)row.Cells["DOB"].Value;
            ls_info.LivestockName = row.Cells["LivestockName"].Value.ToString();
            ls_info.FarmerID=int.Parse( row.Cells["FarmerID"].Value.ToString());
            ls_info.Weight=int.Parse( row.Cells["Weight"].Value.ToString());
            ls_info.Description=row.Cells["Description"].Value.ToString();
            return ls_info;
        }
        public void showSelectedLivestockDetail()
        {
            //
           
            cm_livestockType.SelectedValue = selectedLivestock.LivestockTypeID;
            cm_hfield.SelectedValue = selectedLivestock.HomeFieldID;
            cm_herd.SelectedValue = selectedLivestock.HomeFieldID;

            txt_Name.Text = selectedLivestock.LivestockName;
            txtBreed.Text = selectedLivestock.Breed;
            dtpTime.Value = selectedLivestock.DOB;
            txtMaffid.Text = selectedLivestock.MAFFid+"";
            txtDes.Text = selectedLivestock.Description;
            txt_weight.Text = selectedLivestock.Weight + "";
            for (int i = 0; i < cm_blood.Items.Count; i++)
            {
                if (cm_blood.Items[i].Text == selectedLivestock.Blood)
                {
                    cm_blood.SelectedIndex = i;
                }
            }
            cm_alive.SelectedIndex= selectedLivestock.isAlive == true ? 0 : 1;

        }

        private void UpdateLivestock_Load(object sender, EventArgs e)
        {

        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            try
            {
                //update new information
                selectedLivestock.LivestockName = txt_Name.Text;
                selectedLivestock.LivestockTypeID = int.Parse(cm_livestockType.SelectedValue.ToString());
                selectedLivestock.Blood = cm_blood.SelectedText;
                selectedLivestock.DOB = dtpTime.Value;
                selectedLivestock.HerdID = int.Parse(cm_herd.SelectedValue.ToString());
                selectedLivestock.HomeFieldID = int.Parse(cm_hfield.SelectedValue.ToString());
                selectedLivestock.Description = txtDes.Text;
                selectedLivestock.Breed = txtBreed.Text;
                selectedLivestock.Weight = int.Parse(txt_weight.Text);
                bool ok = livestockManager.EditOneLivestock(selectedLivestock);
                if (ok)
                    MessageBox.Show("Updated");
                else
                    MessageBox.Show("Update fail");
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                string keyword = txtSearch.Text;
                DataTable data = livestockdata.Copy();
                data.Clear();

                for (int i = 0; i < livestockdata.Rows.Count; i++)
                {
                    DataRow aRow = livestockdata.Rows[i];
                    for (int j = 0; j < livestockdata.Columns.Count; j++)
                    {
                        String str = aRow[j].ToString();
                        if (str.Contains(keyword))
                        {
                            DataRow row = data.NewRow();
                            row = aRow;
                            data.Rows.Add(aRow.ItemArray);
                            break;
                        }
                    }
                }
                radGridView_livestock.DataSource = data;
            }
            catch (Exception ex) { }
        }
    }
}
